import pandas as pd
from sklearn import metrics
from copy import copy

def load_easy_instance_from_file(filename):
    easy_data = pd.read_csv(filename)
    easy_pair = {'var_id': 0, 'label': 1}
    easy_pair_list = []
    for i in range(len(easy_data)):
        easy_pair['var_id'] = easy_data['id'][i]
        easy_pair['label'] = easy_data['label'][i]
        easy_pair_list.append(copy(easy_pair))
    return easy_pair_list

dataname = 'songs'
all_true_label = list()
label_file = pd.read_csv(dataname + '_pair_info_g.csv')
for i in label_file.Label:
    all_true_label.append(i)
# easys
easys = load_easy_instance_from_file(dataname + '_easys.csv')

easy_pred = []
easy_true = []
for easy in easys:
    easy_true.append(all_true_label[easy['var_id']])
    easy_pred.append(easy['label'])
# hards
var_set = set()
with open(dataname + '_result_nobalance.txt') as r:
    lines = r.readlines()
    for line in lines:
        var_set.add(int(line.strip('\n').split(' ')[0]))

hards_pred = []
hards_true = []
for line in lines:
    content = line.strip('\n').split(' ')
    var_id = int(content[0])
    label = int(content[2])
    hards_pred.append(label)
    hards_true.append(all_true_label[var_id])
# total = easys + hards
total_true = easy_true + hards_true
total_pred = easy_pred + hards_pred

print("--------------------------------------------")
print("total:")
print("--------------------------------------------")
print("total precision_score: " + str(metrics.precision_score(total_true, total_pred)))
print("total recall_score: " + str(metrics.recall_score(total_true, total_pred)))
print("total f1_score: " + str(metrics.f1_score(total_true, total_pred)))
print("--------------------------------------------")
print("easys:")
print("--------------------------------------------")
print("easys precision_score:" + str(metrics.precision_score(easy_true, easy_pred)))
print("easys recall_score:" + str(metrics.recall_score(easy_true, easy_pred)))
print("easys f1_score: " + str(metrics.f1_score(easy_true, easy_pred)))
print("--------------------------------------------")
print("hards:")
print("--------------------------------------------")
print("hards precision_score: " + str(metrics.precision_score(hards_true, hards_pred)))
print("hards recall_score: " + str(metrics.recall_score(hards_true, hards_pred)))
print("hards f1_score: " + str(metrics.f1_score(hards_true, hards_pred)))